import { deletePublicize, listPublicize } from "@/api/service/publicize";

import { useCloned } from "@vueuse/core";
import { Publicize } from "@/api/service/publicize/types";
import { form, resetForm, showForm } from "./form";
import { keys, query, table } from "./table";

export const list = async () => {
  table.value.loading = true;
  const data = await listPublicize(query.value);
  table.value.data = data.rows.map((e) => {
    e.week = e.week?.split(",");
    return e;
  });
  table.value.loading = false;
};

export const edit = async (record?: Publicize) => {
  if (!record) return;
  form.value = useCloned(record).cloned.value;

  showForm.value = true;
};
export const create = async () => {
  resetForm();
  showForm.value = true;
};
export const del = async (id?: string) => {
  if (id) {
    const ids: string[] = [id];
    await deletePublicize(ids);
    await list();
    keys.value = [];
  }
};
